"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
exports.invoke = invoke;
exports.on = on;
exports.removeAllOn = removeAllOn;
exports.removeOn = removeOn;
exports.setBridgeName = setBridgeName;

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

// @ts-ignore
window.__LIKIT_JS_BRIDGE_NAME__ = 'likitJSBridge';

function getBridge() {
  // 根据不同平台获取bridge并调用
  // @ts-ignore
  var bridge = window[window.__LIKIT_JS_BRIDGE_NAME__] || window.ReactNativeWebView;

  if (!bridge) {
    // @ts-ignore
    var windowWebkit = window.webkit;

    if (windowWebkit) {
      var messageHandlers = windowWebkit.messageHandlers;

      if (messageHandlers) {
        // @ts-ignore
        bridge = messageHandlers[window.__LIKIT_JS_BRIDGE_NAME__] || messageHandlers.ReactNativeWebView;
      }
    }
  }

  return bridge;
}

function getBackBridgeName() {
  // @ts-ignore
  return window.__LIKIT_JS_BRIDGE_NAME__.replace('JSBridge', 'BackJSBridge');
}

;(function () {
  var id = 0;
  var callbacks = {};
  var functions = {};
  window[getBackBridgeName()] = {
    // @ts-ignore
    invoke: function invoke(api, args, callback) {
      var bridge = getBridge();

      if (bridge) {
        // 自增id保持唯一性
        var thisCallbackID; // 存储回调函数

        if (callback) {
          thisCallbackID = (id += 1).toString();
          callbacks[thisCallbackID] = callback;
        } else {
          thisCallbackID = null;
        } // @ts-ignore


        bridge.postMessage(JSON.stringify({
          api: api,
          args: args || {},
          callbackID: thisCallbackID
        }));
      } else if (callback) {
        callback({
          success: false
        });
      }
    },
    onInvoke: function onInvoke(message) {
      var api = message.api;
      var args = message.args || {};
      var callbackID = message.callbackID;
      var responseMessage; // 取出函数，callbackID必然要有，api不一定存在

      if (api) {
        var thisFunctions = functions[api];

        if (thisFunctions) {
          var responseArgs = {};
          var flag = false;
          thisFunctions.forEach(function (_callback) {
            _callback(args, function (_args) {
              flag = true;
              responseArgs = _extends(responseArgs, _args);
            });
          });

          if (flag) {
            responseMessage = JSON.stringify({
              api: api,
              args: responseArgs,
              callbackID: callbackID
            });
            var bridge = getBridge(); // @ts-ignore

            bridge.postMessage(responseMessage);
            return responseMessage;
          }
        }
      } else if (callbackID) {
        var thisFunction = callbacks[callbackID];

        if (thisFunction) {
          responseMessage = JSON.stringify(thisFunction(args) || {});
          delete callbacks[callbackID];
          return responseMessage;
        }
      }

      return undefined;
    },
    on: function on(api, callback) {
      var thisFunctions = functions[api];

      if (!thisFunctions) {
        thisFunctions = [];
        functions[api] = thisFunctions;
      }

      thisFunctions.push(callback);
    },
    removeOn: function removeOn(api, callback) {
      var thisFunctions = functions[api];

      if (thisFunctions) {
        for (var i = 0, length = thisFunctions.length; i < length; i += 1) {
          if (thisFunctions[i] === callback) {
            thisFunctions.splice(i, 1);
            return true;
          }
        }
      }

      return false;
    },
    removeAllOn: function removeAllOn(api) {
      var thisFunctions = functions[api];

      if (thisFunctions) {
        thisFunctions.splice(0, thisFunctions.length);
        return true;
      }

      return false;
    }
  };
})();

function invoke(api, args, callback) {
  // @ts-ignore
  window[getBackBridgeName()].invoke(api, args, callback);
}

function on(api, callback) {
  // @ts-ignore
  window[getBackBridgeName()].on(api, callback);
}

function removeOn(api, callback) {
  // @ts-ignore
  window[getBackBridgeName()].removeOn(api, callback);
}

function removeAllOn(api) {
  // @ts-ignore
  window[getBackBridgeName()].removeAllOn(api);
}

function setBridgeName(identifier) {
  var backBridgeName = getBackBridgeName(); // @ts-ignore

  var jsBridge = window[backBridgeName]; // @ts-ignore

  window[backBridgeName] = undefined; // @ts-ignore

  delete window[backBridgeName]; // @ts-ignore

  window.__LIKIT_JS_BRIDGE_NAME__ = "".concat(identifier, "JSBridge"); // @ts-ignore

  window[getBackBridgeName()] = jsBridge;
}

var _default = {
  invoke: invoke,
  on: on,
  removeOn: removeOn,
  removeAllOn: removeAllOn,
  setBridgeName: setBridgeName
};
exports["default"] = _default;